package edu.unlv.cs.ladders.entities;

import edu.unlv.cs.ladders.entities.Participant;
import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect ParticipantDataOnDemand_Roo_DataOnDemand {
    
    declare @type: ParticipantDataOnDemand: @Component;
    
    private Random ParticipantDataOnDemand.rnd = new SecureRandom();
    
    private List<Participant> ParticipantDataOnDemand.data;
    
    public Participant ParticipantDataOnDemand.getNewTransientParticipant(int index) {
        edu.unlv.cs.ladders.entities.Participant obj = new edu.unlv.cs.ladders.entities.Participant();
        obj.setAccountNonExpired(true);
        obj.setAccountNonLocked(true);
        obj.setCredentialsNonExpired(true);
        obj.setEnabled(true);
        obj.setName("name_" + index);
        obj.setPassword("password_" + index);
        obj.setPasswordEncoder(null);
        obj.setPhone("phone_" + index);
        obj.setSalt("salt_" + index);
        return obj;
    }
    
    public Participant ParticipantDataOnDemand.getSpecificParticipant(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Participant obj = data.get(index);
        return Participant.findParticipant(obj.getId());
    }
    
    public Participant ParticipantDataOnDemand.getRandomParticipant() {
        init();
        Participant obj = data.get(rnd.nextInt(data.size()));
        return Participant.findParticipant(obj.getId());
    }
    
    public boolean ParticipantDataOnDemand.modifyParticipant(Participant obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void ParticipantDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = edu.unlv.cs.ladders.entities.Participant.findParticipantEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Participant' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<edu.unlv.cs.ladders.entities.Participant>();
        for (int i = 0; i < 10; i++) {
            edu.unlv.cs.ladders.entities.Participant obj = getNewTransientParticipant(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
